package cn.com.surker.workcon.service.siwei;

import cn.com.surker.base.system.entity.SysLine;
import cn.com.surker.database.JDBCTemplateCache;
import cn.com.surker.production.entity.vo.ProductionIndentVo;
import cn.com.surker.workcon.common.GKJDBCService;
import cn.com.surker.workcon.common.RecipeTranEntity;
import cn.com.surker.workcon.service.RecipeService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 思维上传配合比接口
 * 思维的上传配合比是根据工控机任务单来的,需要先将任务单转正供上传工控机后,在传配合比
 *
 * @author luotao
 * @date 2025/3/5
 */
public class RecipeServiceImpl extends GKJDBCService implements RecipeService {

	@Override
	public Long uploadExpend(SysLine line, ProductionIndentVo indent, String code, String betLev, String filter, String freeze, String season, Integer mixLast, String scale, String exper,
							 String assoss, List<RecipeTranEntity> list, Date nowDate) {
		if (line == null || indent == null) {
			return -1l;
		}
		JdbcTemplate jdbcTemplate = JDBCTemplateCache.getLineTransactionJdbcTemp(line);
		if (jdbcTemplate == null) {
			return -1l;
		}
		//	砼的任务单
		String concreteRwdCode = indent.getCode();
		//	砂浆的任务单号
		String standRwdCode = "R" + concreteRwdCode;
		//	砂浆的配合比编号(在原配合比编号上加R)
		String standPhbCode = "R" + code;
		//	查询是否存在任务单
		String queryRwdhSql = "select FRwdh from trwd where FRWNO = ?";
		Object[] queryRhdh = {concreteRwdCode};
		List<Map<String, Object>> cRwdList = jdbcTemplate.queryForList(queryRwdhSql, queryRhdh);
		if (cRwdList == null || cRwdList.isEmpty()) {
			//	没有任务单,
			System.err.println("思维没有任务单1:" + concreteRwdCode);
			return -1l;
		}
		//	取出任务单数据
		Map<String, Object> crwd = cRwdList.get(0);
		if (crwd == null) {
			System.err.println("思维没有任务单2:" + concreteRwdCode);
			return -1l;
		}
		Object cRwdhObj = crwd.get("FRwdh");
		if (cRwdhObj == null) {
			System.err.println("思维没有任务单3:" + concreteRwdCode + "," + JSONObject.toJSONString(crwd));
			return -1l;
		}
		//	查询砂浆任务单
		Object[] queryStandRhdh = {standRwdCode};
		List<Map<String, Object>> sRwdList = jdbcTemplate.queryForList(queryRwdhSql, queryStandRhdh);
		if (cRwdList == null || cRwdList.isEmpty()) {
			System.err.println("思维没有砂浆任务单1:" + standRwdCode);
			return -1l;
		}
		Map<String, Object> sRwd = sRwdList.get(0);
		if (sRwd == null) {
			System.err.println("思维没有砂浆任务单2:" + standRwdCode);
		}
		Object sRwdhObj = sRwd.get("FRwdh");
		if (sRwdhObj == null) {
			System.err.println("思维没有任务单3:" + standRwdCode + "," + JSONObject.toJSONString(sRwdhObj));
			return -1l;
		}
		//	砼任务单id
		Long cRwdh = Long.valueOf(cRwdhObj.toString());
		//	砂浆任务单号
		Long sRwdh = Long.valueOf(sRwdhObj.toString());
		//	砼的配合比
		String concreteFbz = "";
		//	砂浆的配合比
		String standFbz = "";
		//	砼配合比原材料数据
		List<Object[]> concreteMaterials = new ArrayList<>();
		//	砂浆配合比原材料数据
		List<Object[]> sandMaterials = new ArrayList<>();
		//	凭借砼的配合比
		for (RecipeTranEntity recipe : list) {
			//	工控机的仓位
			String controlStorage = recipe.getControlStorage();
			//	砼用量
			Double realDosage = recipe.getConcreteDosage();
			//	砂浆用量
			Double standNum = recipe.getStandDosage();
			//	砼用量
			concreteFbz += "{" + controlStorage + "()=" + realDosage + "}";
			//	单个的配合比原材料数据
			Object[] conMaterial = {cRwdh, controlStorage, realDosage};
			concreteMaterials.add(conMaterial);
			//	砂浆用量
			standFbz += "{" + controlStorage + "()=" + standNum + "}";
			Object[] stanMaterial = {sRwdh, controlStorage, standNum};
			sandMaterials.add(stanMaterial);
		}
		//开启事务
		JDBCTemplateCache.beginTransaction(line.getId() + "", 0);
		try {
			//	插入砼信息
			insertPhb(jdbcTemplate, cRwdh, nowDate, concreteFbz);
			insertPhbYcl(jdbcTemplate, cRwdh, concreteMaterials);
			//	插入砂浆信息
			insertPhb(jdbcTemplate, sRwdh, nowDate, standFbz);
			insertPhbYcl(jdbcTemplate, sRwdh, sandMaterials);
		} catch (Exception e) {
			e.printStackTrace();
			//	回滚事务
			JDBCTemplateCache.rollbackLine(line.getId() + "", 0);//回滚事务
			return -1l;
		}
		JDBCTemplateCache.commitTranLine(line.getId() + "", 0);//提交事务
		return 1l;
	}

	//	插入配合比数据
	private void insertPhb(JdbcTemplate jdbcTemplate, Long frwdh, Date datTim, String fbz) {
		// 插入任务单下的配合比数据
		String fpblx = "施工";
		String insertPhbSql = "insert into trwdphb (FRwdh,FTzsj,fpblx,FBz) values (?,?,?,?)\n";
		//	配合比参数
		Object[] phbParams = {frwdh, datTim, fpblx, fbz};
		//	插入任务单配合比
		jdbcTemplate.update(insertPhbSql, phbParams);
	}

	private void insertPhbYcl(JdbcTemplate jdbcTemplate, Long frwdh, List<Object[]> materials) {
		//	删除旧数据
		String delYclSql2 = "delete from trwdphbycl where FRwdh = " + frwdh;
		jdbcTemplate.update(delYclSql2);
		//	新增数据
		String insertYclSql2 = "insert into trwdphbycl (FRwdh,FYlmc,FSysl)\n" +
			"values (?,?,?)\n";
		jdbcTemplate.batchUpdate(insertYclSql2, materials);
	}
}
